package com.base.web.core.common;


import com.base.web.core.enums.ResultEmun;

/**
 * Created with IntelliJ IDEA.
 * Description: json结果返回封装
 * User: Mario
 * Date: 2018-06-18 14:02
 */
public class JsonResult {

    /**
     * 返回编码"200"=成功；"500"=失败
     */
    public String status;

    /**
     * layui前端解析需要code字段为0，和业务无关
     */
    public int code = 0;

    /**
     * layui前端解析需要数据条数，与业务无关
     */
    public Long count;


    /**
     * 返回信息
     */
    public String msg;

    /**
     * 返回数据
     */
    public Object data;

    public JsonResult() {
    }

    public JsonResult(Object data) {
        this.data = data;
    }

    public JsonResult(Object data, ResultEmun resultEmun) {
        this.data = data;
        this.status = resultEmun.getCode();
        this.msg = resultEmun.getMessage();
    }

    public JsonResult(ResultEmun resultEmun) {
        this.status = resultEmun.getCode();
        this.msg = resultEmun.getMessage();
    }

    public JsonResult(Object data,String code,String msg) {
        this.data = data;
        this.status = code;
        this.msg = msg;

    }

    public JsonResult(Object data, ResultEmun resultEmun, Long count) {
        this.data = data;
        this.status = resultEmun.getCode();
        this.msg = resultEmun.getMessage();
        this.count = count;
    }

    /**
     * 逻辑操作成功结果封装
     *
     * @param data 结果数据
     * @return
     */
    public static JsonResult success(Object data) {
        return new JsonResult(data, ResultEmun.SUCCESS);
    }

    /**
     * 逻辑操作成功结果封装,无数据
     * @return
     */
    public static JsonResult success() {
        return new JsonResult(ResultEmun.SUCCESS);
    }

    /**
     * 逻辑操作成功结果封装,无数据
     * @return
     */
    public static JsonResult fail() {
        return new JsonResult(ResultEmun.FAIL);
    }

    /**
     * layui列表查询成功结果封装
     *
     * @param data  列表数据
     * @param count 数据个数
     * @return
     */
    public static JsonResult success(Object data, Long count) {
        return new JsonResult(data, ResultEmun.SUCCESS, count);
    }

    /**
     * 处理失败结果封装（自定义错误信息）
     * @param msg 错误信息
     * @return
     */
    public static JsonResult fail(String msg) {
        return new JsonResult(null, BaseConstant.FAIL_CODE, msg);
    }


    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "JsonResult{" +
                "status='" + status + '\'' +
                ", code=" + code +
                ", count=" + count +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
